package cn.ztydata.teachingsystem.heaven.web.controller;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.heaven.cons.MsgTemplate;
import cn.ztydata.teachingsystem.heaven.entity.Daily;
import cn.ztydata.teachingsystem.heaven.entity.User;
import cn.ztydata.teachingsystem.heaven.service.DailysService;
import cn.ztydata.teachingsystem.heaven.web.dto.SpecialtyGroup.DailyDto;
import cn.ztydata.teachingsystem.heaven.web.wrapper.HttpSessionProxy;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResult;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultData;
import cn.ztydata.teachingsystem.webservice.result.json.JsonResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;

/**
 * 教学日志控制器
 *
 * @author lyj
 * @since 2015/5/6
 */
@RequestMapping(value = "/daily")
@Controller
public class DailysController extends BaseController {
    @Autowired
    private DailysService dailysService;

    @Value("${session.key}")
    private String sessionKey;


    //<editor-fold desc="新增">

    /**
     * 新增日志
     *
     * @param dailyDto
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addDaily(@RequestBody DailyDto dailyDto) {
        Assert.notNull(dailyDto.getMajorId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));

        dailysService.addDaily(mapper.map(dailyDto, Daily.class));

        log.debug("新增日志");
        return new JsonResult(true, "新增日志成功！");
    }
   //</editor-fold>

    //<editor-fold desc="删除">

    /**
     * 删除日志
     *
     * @param id 日志ID
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult deleteById(@RequestParam("id") long id) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));

        dailysService.deleteById(id);

        log.debug("删除日志");
        return new JsonResult(true, "删除日志成功！");
    }
    //</editor-fold>

    //<editor-fold desc="编辑">

    /**
     * 编辑日志
     *
     * @param dailyDto
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult updateDaily(@RequestBody DailyDto dailyDto) {
        Assert.notNull(dailyDto.getId(), MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));

        dailysService.updateDaily(mapper.map(dailyDto, Daily.class));

        log.debug("编辑日志");
        return new JsonResult(true, "编辑日志成功！");
    }

    /**
     * 是否公开
     *
     * @param id   日志ID
     * @param open 是否公开
     * @return JsonResult
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/open", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult updateOpenById(@RequestParam("id") long id,
                                     @RequestParam("status") int open) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));
        Assert.isTrue(open == 0 || open == 1, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "是否公开标记"));

        dailysService.updateOpenById(id, open);

        log.debug("是否公开");
        return new JsonResult(true, "操作成功！");
    }
    //</editor-fold>

    //<editor-fold desc="查询">

    /**
     * 教学日志列表
     *
     * @param groupId  群组ID
     * @param pageNum  当前页码
     * @param pageSize 每页条数
     * @param tag      标记 0全部 1我的
     * @return JsonResultPage
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultPage getAllDailies(@RequestParam("groupId") long groupId,
                                        @RequestParam("page") int pageNum,
                                        @RequestParam("pageSize") int pageSize,
                                        @RequestParam("tag") int tag,
                                        HttpServletRequest request) {
        Assert.isTrue(groupId > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "群组ID"));
        Assert.isTrue(pageNum > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "当前页码"));
        Assert.isTrue(pageSize > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "每页条数"));
        Assert.isTrue(tag == 0 || tag == 1, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "tag标记"));

        //获取登录用户信息
        User userInfo = (User)((HttpSessionProxy)request.getSession()).getAttribute(sessionKey);

        Page<Daily> page = dailysService.getAllDailies(groupId, pageNum, pageSize, tag, userInfo);

        log.debug("教学日志列表");
        return new JsonResultPage(new Page(page.getTotal(), pageSize, pageNum, mapper.mapList(page.getItems(), DailyDto.class)));
    }


    /**
     * 跟据ID获取日志明细
     *
     * @param id 日志ID
     * @return JsonResultData
     * @author lyj
     * @since 2015-5-6
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public JsonResultData getDetailById(@RequestParam("id") long id) {
        Assert.isTrue(id > 0, MessageFormat.format(MsgTemplate.ARG_VALIDATION_ERR, "日志ID"));

        log.debug("跟据ID获取日志明细");
        return new JsonResultData(mapper.map(dailysService.getDetailById(id), DailyDto.class));

    }
    //</editor-fold>
}
